/*
 * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
 * All rights reserved.
 *
 * This file is part of Harmattan SmashMiner OpenGL game application.
 * 
 * Harmattan SmashMiner OpenGL Example Application version 1.0.0
 *  
 * Latest update: 15.4.2011
 *
 * The Harmattan SmashMiner OpenGL example application demonstrates how to use 
 * the OpenGL ES in Harmattan devices.
 *
 * This example is provided as a starting point for 3rd party
 * developers (you) to ease the implementation of OpenGL based 
 * games. Ideas, parts of code or methodologies that this
 * example application uses can be freely used without restrictions.
 *
 * See file README.txt how to build and compile this example application
 * in the Harmattan SDK environment. 
 * 
 * See file INSTALL.txt to find out what is required to run this
 * application and how to install the application to the device or
 * alternatively in QEMU-emulator.
 *
 * The LICENSE.txt file explains the license this software is provided
 * with. A copy of the same license is included in this source file.
 * 
 */

/*
 * Copyright (C) 2011 by Nokia Corporation.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */

#include "highscoremodel.h"

/*!
  \class HighScoreItem

  Data item for single high score.

  \sa HighScoreModel
*/

//
// HighScoreItem implementation
//
/*!
 Constructs empty HighScoreItem
*/
HighScoreItem::HighScoreItem() {}

/*!
 Constructs the HighScoreItem with given values.
*/
HighScoreItem::HighScoreItem(
        int rank,
        const QString &name,
        const QString &score,
        const QString &time,
        const QString &date) 
    : rankValue(rank)
    , nameValue(name)
    , scoreValue(score)
    , timeValue(time)
    , dateValue(date)
{
}

/*!
 Constructs copy of the HighScoreItem.
*/
HighScoreItem::HighScoreItem(const HighScoreItem &item)
    : QDeclarativeItem()
    , rankValue(item.rank())
    , nameValue(item.name())
    , scoreValue(item.score())
    , timeValue(item.time())
    , dateValue(item.date())
{
}

/*!
 Rank of the highscore.
*/
int HighScoreItem::rank() const { return rankValue; }

/*!
 Player name.
*/
QString HighScoreItem::name() const { return nameValue; }

/*!
 Player score.
*/
QString HighScoreItem::score() const { return scoreValue; }

/*!
 Playtime.
*/
QString HighScoreItem::time() const { return timeValue; }

/*!
 Date of the score.
*/
QString HighScoreItem::date() const { return dateValue; }

//
// HighScoreModel implementation
//
/*!
  \class HighScoreModel
  Data model for high scores. Used from HighScores.qml view to show high scores list.
  \sa HighScoreItem
*/

/*!
 Constructs HighScore model.
*/
HighScoreModel::HighScoreModel(QObject *parent) :
        QAbstractListModel(parent)
{
    QHash<int, QByteArray> role;
    role[RankRole] = "scoreRank";
    role[NameRole] = "scoreName";
    role[ScoreRole] = "scoreValue";
    role[TimeRole] = "scoreTime";
    role[DateRole] = "scoreDate";
    setRoleNames(role);
}

/*!
 Destructs HighScore model.
*/
HighScoreModel::~HighScoreModel()
{
    qDeleteAll(items);
}

/*!
 Add HighScoreItem HighScore model. Ownership is transferred.
*/
void HighScoreModel::addItem(const HighScoreItem *item)
{
    beginInsertRows(QModelIndex(), rowCount(), rowCount());
    items << item;
    endInsertRows();
}

/*!
 Number of rows.
*/
int HighScoreModel::rowCount(const QModelIndex &/*parent*/) const
{
    return items.count();
}

/*!
 Get data item based on role.
 \sa HighScoreRoles
*/
QVariant HighScoreModel::data(const QModelIndex &index, int role) const
{
    if (index.row() < 0 || index.row() > items.count())
        return QVariant();

    const HighScoreItem *item = items[index.row()];

    switch (role) {
        case RankRole: return item->rank();
        case NameRole: return item->name();
        case ScoreRole: return item->score();
        case TimeRole: return item->time();
        case DateRole: return item->date();
        default: return QVariant();
    }
}

/*!
 Updates model based on the \a highScoreList.
*/
void HighScoreModel::updateModel(QList<GameSettings::HighScore> &highScoreList)
{
    beginResetModel();
    qDeleteAll(items);
    items.clear();

    for (int i = 0; i < highScoreList.size(); ++i) {
        QString scoreString = QString::number(highScoreList.at(i).score, 10);
        QString dateString = highScoreList.at(i).date.toString("dd-MM-yyyy");
        HighScoreItem *item = new HighScoreItem(i+1,
            highScoreList.at(i).name,
            scoreString,
            highScoreList.at(i).time,
            dateString);
        addItem(item);
    }
   endResetModel();
}

/*!
 Get single row.
*/
QVariantMap HighScoreModel::get(int row) const
{
    if (row < 0 || row >= items.count())
        return QVariantMap();

    const HighScoreItem *item = items[row];

    return itemToQVariantMap(item);
}

/*!
 HighScoreItem to QVariantMap.
*/
QVariantMap HighScoreModel::itemToQVariantMap(const HighScoreItem *item) const
{
    QVariantMap map;
    map["scoreRank"] = QVariant(item->rank());
    map["scoreName"] = QVariant(item->name());
    map["scoreValue"] = QVariant(item->score());
    map["scoreTime"] = QVariant(item->time());
    map["scoreDate"] = QVariant(item->date());
    return map;
}
